<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 查字典（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>输入一个单词前缀和一个字典&#xff0c;输出包含该前缀的单词</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>单词前缀&#43;字典长度&#43;字典<br /> 字典是一个有序单词数组<br /> 输入输出都是小写</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>所有包含该前缀的单词&#xff0c;多个单词换行输出</p> 
<p>若没有则返回-1</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">b 3 a b c</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">b</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">abc 4 a ab abc abcd</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;"> <p>abc</p> <p>abcd</p> </td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">a 3 b c d</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">-1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>这题200分的&#xff0c;但是感觉很简单&#xff0c;难道有坑&#xff1f;</p> 
<p>我感觉这题就是考察字符串的基本操作&#xff0c;一个startsWith方法。</p> 
<p></p> 
<p>Java解法中&#xff0c;考虑大数量级&#xff0c;输入处理不使用next&#xff0c;nextInt获取&#xff0c;而是用nextLine整行获取。</p> 
<hr /> 
<p>2023.07.29</p> 
<p>仅仅用startsWith方法通过率只有80%&#xff0c;</p> 
<p>有考友反馈是没有判断word和prefix的长度关系&#xff0c;即当word.length &lt; prefix.length&#xff0c;则不需要进行startsWith判断。</p> 
<p>但是startsWith底层其实是优先做了这个判断的&#xff0c;比如Java源码中&#xff1a;</p> 
<p><img alt="" height="411" src="https://img-blog.csdnimg.cn/3ac127d188a547758aa442b158b81862.png" width="800" /></p> 
<p>因此&#xff0c;判断如果使用了startsWith&#xff0c;则判断word.length &lt; prefix.length是冗余的。</p> 
<p></p> 
<p>本题的问题应该是&#xff0c;我将输入获取的内容存储起来&#xff0c;然后传入了核心代码getResult中&#xff0c;因此可能超出了内存限制。</p> 
<p>这题我们应该边获取输入&#xff0c;边判断&#xff0c;这样才是内存最优的。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const tmp &#61; line.split(&#34; &#34;);
  const prefix &#61; tmp[0];

  let find &#61; false;
  const n &#61; parseInt(tmp[1]);

  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    const word &#61; tmp[i &#43; 2];

    if (word.startsWith(prefix)) {
      find &#61; true;
      console.log(word);
    }
  }

  if (!find) console.log(-1);
});
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String prefix &#61; sc.next();

    boolean find &#61; false;

    int n &#61; sc.nextInt();
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      String word &#61; sc.next();

      if (word.startsWith(prefix)) {
        find &#61; true;
        System.out.println(word);
      }
    }

    if (!find) System.out.println(-1);
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
tmp &#61; input().split()

prefix &#61; tmp[0]
n &#61; int(tmp[1])

find &#61; False
for i in range(n):
    word &#61; tmp[i&#43;2]

    if word.startswith(prefix):
        find &#61; True
        print(word)

if not find:
    print(-1)
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define MAX_LEN 1000

int main() {
    char prefix[MAX_LEN];
    scanf(&#34;%s&#34;, prefix);

    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int isFind &#61; 0;

    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        char word[MAX_LEN];
        scanf(&#34;%s&#34;, word);

        if(strncmp(prefix, word, strlen(prefix)) &#61;&#61; 0) {
            isFind &#61; 1;
            puts(word);
        }
    }

    if(!isFind) {
        puts(&#34;-1&#34;);
    }

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>